import os
import random


# 练习
# 创建3000个文件 目录规则 四位(0001,3000)_种类_9位的十六进制
def fix(i, total_len):
    return f"{i:04}"
    # i_len = len(str(i))
    # fix_len = total_len - i_len
    # if fix_len > 0:
    #     return f"0" * fix_len + str(i)
    # else:
    #     return str(i)


tmp_path = "tmp_path"


def gen(num, type_list_1, randLen):
    if not os.path.isdir(tmp_path):
        os.mkdir(tmp_path)
    for i in range(1, num + 1):
        prefix = fix(i, 4)
        type_name = random.choice(type_list_1)
        prefix2 = prefix + "_" + type_name
        # print(prefix2)
        suffix = []
        for _ in range(randLen):
            r = hex(random.randint(0, 15))[2:]
            suffix.append(str.upper(r))
        prefix3 = prefix2 + "_" + "".join(suffix)
        write_file_path = os.path.join(tmp_path, prefix3)
        print("writer", write_file_path)
        with open(write_file_path, "w"):
            pass


def gen2(num, type_list2, randLen):
    if not os.path.isdir(tmp_path):
        os.mkdir(tmp_path)
    for i in range(1, num + 1):
        prefix = fix(i, 4)
        type_name = random.choice(type_list2)
        prefix2 = prefix + "_" + type_name
        # print(prefix2)
        suffix = []
        for _ in range(randLen):
            suffix.append(random.choice(suffix_list))
        prefix3 = prefix2 + "_" + "".join(suffix)
        write_file_path = os.path.join(tmp_path, prefix3)
        print("writer", write_file_path)
        with open(write_file_path, "w"):
            pass


def del_file(del_dir):
    for root, dirs, files in os.walk(del_dir):
        for file in files:
            if file.endswith(".py") or file.endswith(".txt") or file.endswith(".jpg"):
                continue
            else:
                del_file_path = os.path.join(root, file)
                print("del_file_path=", del_file_path)
                os.remove(del_file_path)


if __name__ == '__main__':
    del_file(tmp_path)
    if os.path.isdir(tmp_path):
        os.removedirs(tmp_path)
     type_list = ["水果", "烟酒", "粮油", "蛋类", "蔬菜"]
    # # gen(3000, type_list, 9)
    suffix_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
    gen2(3000, type_list, 9)
